package com.example.wudi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wudi.dao.RoleDao;
import com.example.wudi.entity.Permissions;
import com.example.wudi.entity.Role;
import com.example.wudi.entity.User;
import com.example.wudi.mapper.PermissionsMapper;
import com.example.wudi.mapper.RoleMapper;
import com.example.wudi.mapper.RouterMapper;
import com.example.wudi.mapper.UserMapper;
import com.example.wudi.params.AddOrUpdateRoleParams;
import com.example.wudi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionsMapper permissionsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Role> findAll() {
        final List<Role> roles = roleMapper.selectList(null);
        return roles;
    }

    @Override
    public RoleDao getRoleByUserId(String roleId) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        final Role role = roleMapper.selectOne(queryWrapper);
        final String permissionsId = role.getPermissionsId();
        final String[] split = permissionsId.split(",");
        List<Permissions> list = new ArrayList<>();
        RoleDao roleDao = new RoleDao();

        for (int i = 0; i< split.length;i++) {
            String item = split[i];
            QueryWrapper<Permissions> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", item);
            final Permissions permissions = permissionsMapper.selectOne(queryWrapper1);
            System.out.println(permissions);
            list.add(permissions);
        }
        roleDao.setRoleId(role.getRoleId());
        roleDao.setRoleName(role.getRoleName());
        roleDao.setCreateTime(role.getCreateTime());
        List list1 = new ArrayList();
        roleDao.setPermissionsList(list.get(0) == null ? list1 : list);
        return roleDao;
    }

    @Override
    public int updateRole(AddOrUpdateRoleParams addOrUpdateRoleParams) {
        QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id", addOrUpdateRoleParams.getRoleId());
        final Role role1 = roleMapper.selectOne(queryWrapper1);
        if((role1.getPermissionsId().split(",").length > addOrUpdateRoleParams.getPermissionList().split(",").length) && addOrUpdateRoleParams.getRoleId().equals("8")) {
            return -999;
        }
        Role role = new Role();
        role.setRoleId(addOrUpdateRoleParams.getRoleId());
        role.setRoleName(addOrUpdateRoleParams.getRoleName());
        role.setCreateTime(addOrUpdateRoleParams.getCreateDate());
        role.setPermissionsId(addOrUpdateRoleParams.getPermissionList());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", addOrUpdateRoleParams.getRoleId());
        return roleMapper.update(role, queryWrapper);
    }

    @Override
    public int addRole(AddOrUpdateRoleParams addOrUpdateRoleParams) {
        Role role = new Role();
        role.setRoleName(addOrUpdateRoleParams.getRoleName());
        role.setCreateTime(addOrUpdateRoleParams.getCreateDate());
        role.setPermissionsId(addOrUpdateRoleParams.getPermissionList());
        return roleMapper.insert(role);
    }

    @Override
    public int deleteRole(String id) {
        if(id.equals("8") || id.equals("9")) {
            return -999;
        }
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id", id);
        final List<User> users = userMapper.selectList(queryWrapper1);
        for (int i =0; i < users.size(); i++) {
            final User item = users.get(i);
            item.setRoleId("9");
            QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("id", item.getId());
            userMapper.update(item, queryWrapper2);
        }
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);

        return roleMapper.delete(queryWrapper);
    }
}
